home *** CD-ROM | disk | FTP | other *** search
/ Borland JBuilder 6 / jbuilder6.iso / IBM VisualAge for Java Enterprise v4.0 Retail / ivj40 / setup / REP.Cab / F25739_JavaSound.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-03-27  |  6.6 KB  |  186 lines

  1. package demos.Mix;
  2.  
  3. import java.applet.Applet;
  4. import java.applet.AudioClip;
  5. import java.awt.BorderLayout;
  6. import java.awt.Color;
  7. import java.awt.Component;
  8. import java.awt.Container;
  9. import java.awt.Dimension;
  10. import java.awt.FlowLayout;
  11. import java.awt.Font;
  12. import java.awt.GridBagConstraints;
  13. import java.awt.GridBagLayout;
  14. import java.awt.GridLayout;
  15. import java.awt.Window;
  16. import java.awt.event.ActionEvent;
  17. import java.awt.event.ActionListener;
  18. import java.awt.event.WindowListener;
  19. import java.net.URL;
  20. import javax.swing.AbstractButton;
  21. import javax.swing.ImageIcon;
  22. import javax.swing.JButton;
  23. import javax.swing.JComponent;
  24. import javax.swing.JFrame;
  25. import javax.swing.JLabel;
  26. import javax.swing.JPanel;
  27. import javax.swing.JTabbedPane;
  28. import javax.swing.border.BevelBorder;
  29.  
  30. public class JavaSound extends JPanel implements ActionListener {
  31.    private AudioClip clip = null;
  32.    private JButton playButton;
  33.    private JButton loopButton;
  34.    private Timer timer = null;
  35.    protected static final int STOP = 0;
  36.    protected static final int PLAY = 1;
  37.    protected static final int LOOP = 2;
  38.    protected int audioState = 0;
  39.    // $FF: renamed from: f1 java.awt.Font
  40.    private Font field_0 = new Font("serif", 3, 18);
  41.    // $FF: renamed from: f2 java.awt.Font
  42.    private Font field_1 = new Font("serif", 0, 14);
  43.    static Class class$demos$Mix$JavaSound;
  44.  
  45.    public JavaSound() {
  46.       ((Container)this).setLayout(new BorderLayout());
  47.       ((JComponent)this).setBackground(Color.gray);
  48.       String[] playType = new String[]{"mix1", "mix2", "rmf"};
  49.       String[][] playList = new String[][]{{"sfx-medley.rmf", "trippygaia1.mid", "spacemusic.au"}, {"bong.wav", "bark.aiff", "trance.rmf"}, {"ambient.rmf", "classical.rmf", "modern-rock.rmf"}};
  50.       JTabbedPane tabbedPane = new JTabbedPane();
  51.       ((JComponent)tabbedPane).setBorder(new BevelBorder(0));
  52.       ((JComponent)tabbedPane).setFont(new Font("Times New Roman", 0, 10));
  53.  
  54.       for(int i = 0; i < playType.length; ++i) {
  55.          JPanel jp = new JPanel(new GridLayout(0, 1));
  56.  
  57.          for(int j = 0; j < playList[i].length; ++j) {
  58.             ((Container)jp).add(new JavaSound(this, playList[i][j], false));
  59.          }
  60.  
  61.          tabbedPane.addTab(playType[i], jp);
  62.       }
  63.  
  64.       ((Container)this).add(tabbedPane);
  65.       JPanel jp1 = new JPanel(new GridLayout(0, 1));
  66.       String[] s1 = new String[]{"J", "A", "V", "A"};
  67.  
  68.       for(int i = 0; i < s1.length; ++i) {
  69.          ((Container)jp1).add(this.addLabel(s1[i]));
  70.       }
  71.  
  72.       ((Container)this).add("West", jp1);
  73.       JPanel jp2 = new JPanel(new GridLayout(0, 1));
  74.       String[] s2 = new String[]{"S", "O", "U", "N", "D"};
  75.  
  76.       for(int i = 0; i < s2.length; ++i) {
  77.          ((Container)jp2).add(this.addLabel(s2[i]));
  78.       }
  79.  
  80.       ((Container)this).add("East", jp2);
  81.    }
  82.  
  83.    public JavaSound(JavaSound js, String name, boolean playSomeAudio) {
  84.       Class var10000 = class$demos$Mix$JavaSound;
  85.       if (var10000 == null) {
  86.          try {
  87.             var10000 = Class.forName("demos.Mix.JavaSound");
  88.          } catch (ClassNotFoundException var9) {
  89.             throw new NoClassDefFoundError(((Throwable)var9).getMessage());
  90.          }
  91.  
  92.          class$demos$Mix$JavaSound = var10000;
  93.       }
  94.  
  95.       URL url = var10000.getResource("audio/" + name);
  96.       this.clip = Applet.newAudioClip(url);
  97.       GridBagLayout gbl = new GridBagLayout();
  98.       ((Container)this).setLayout(gbl);
  99.       ((JComponent)this).setBackground(Color.gray);
  100.       JLabel l = new JLabel(name);
  101.       ((JComponent)l).setFont(this.field_1);
  102.       ((JComponent)l).setForeground(Color.black);
  103.       GridBagConstraints gbc = new GridBagConstraints();
  104.       gbc.gridx = 0;
  105.       gbc.gridy = 0;
  106.       gbl.setConstraints(l, gbc);
  107.       ((Container)this).add(l);
  108.       JPanel p1 = new JPanel(new FlowLayout());
  109.       ((JComponent)p1).setBackground(Color.gray);
  110.       this.playButton = new JButton("play");
  111.       this.playButton.setFont(this.field_1);
  112.       this.playButton.setBackground(Color.green);
  113.       this.playButton.addActionListener(this);
  114.       ((Container)p1).add(this.playButton);
  115.       this.loopButton = new JButton("loop");
  116.       this.loopButton.setFont(this.field_1);
  117.       this.loopButton.addActionListener(this);
  118.       ((Container)p1).add(this.loopButton);
  119.       this.timer = new Timer(this, js, this, this.clip);
  120.       ((Container)p1).add(this.timer);
  121.       gbc = new GridBagConstraints();
  122.       gbc.gridx = 0;
  123.       gbc.gridy = 1;
  124.       gbl.setConstraints(p1, gbc);
  125.       ((Container)this).add(p1);
  126.       if (playSomeAudio) {
  127.          this.actionPerformed(new ActionEvent(this, 1001, "play"));
  128.       }
  129.  
  130.    }
  131.  
  132.    public void actionPerformed(ActionEvent evt) {
  133.       if (evt.getActionCommand() == "loop") {
  134.          this.clip.loop();
  135.          this.timer.start();
  136.          this.audioState = 2;
  137.          this.playButton.setText("stop");
  138.          this.playButton.setBackground(Color.red);
  139.       } else if (this.audioState >= 1) {
  140.          this.stopAudio();
  141.       } else {
  142.          this.playAudio();
  143.       }
  144.  
  145.    }
  146.  
  147.    public JButton addButton(ImageIcon ii) {
  148.       JButton b = new JButton(ii);
  149.       ((AbstractButton)b).addActionListener(this);
  150.       return b;
  151.    }
  152.  
  153.    public JLabel addLabel(String s) {
  154.       JLabel l = new JLabel(" " + s + " ");
  155.       ((JComponent)l).setForeground(Color.gray);
  156.       ((JComponent)l).setFont(this.field_0);
  157.       return l;
  158.    }
  159.  
  160.    public static void main(String[] s) {
  161.       WindowListener l = new 1();
  162.       JFrame f = new JFrame("Java2D Demo - JavaSound");
  163.       ((Window)f).addWindowListener(l);
  164.       f.getContentPane().add("Center", new JavaSound());
  165.       ((Window)f).pack();
  166.       ((Component)f).setSize(new Dimension(320, 260));
  167.       ((Window)f).show();
  168.    }
  169.  
  170.    public void playAudio() {
  171.       this.clip.play();
  172.       this.playButton.setText("stop");
  173.       this.playButton.setBackground(Color.red);
  174.       this.timer.start();
  175.       this.audioState = 1;
  176.    }
  177.  
  178.    public void stopAudio() {
  179.       this.audioState = 0;
  180.       this.clip.stop();
  181.       this.playButton.setText("play");
  182.       this.playButton.setBackground(Color.green);
  183.       this.timer.stop();
  184.    }
  185. }
  186.